/*认证考试公共js*/
//获取html代码中的试题,并拼接成数组
function GetTestData(list) {
	var data = list;
	var dataArr = new Array();
	var danArr = new Array();
	var duoArr = new Array();
	var panArr = new Array();
	var shituArr = new Array();
	var paixuArr = new Array();
	var shipinArr = new Array();
	var lunshuArr = new Array();
	var anliArr = new Array();
	var chuanArr = new Array();
	var tkArr = new Array();
	var tbArr = new Array();
	var scArr = new Array();
	//实操
	if (data != null && data != '') {
		if (data.dan != undefined && data.dan.length > 0 && data.dan != '' && data.dan != null) {
			for (var i = 0; i < data.dan.length; i++) {
				var weeklyData = data.dan[i];
				danArr.push(weeklyData);
			}
		}
		if (data.duo != undefined && data.duo.length > 0 && data.duo != '' && data.duo != null) {
			for (var i = 0; i < data.duo.length; i++) {
				var duoData = data.duo[i];
				duoArr.push(duoData);
			}
		}
		if (data.pan != undefined && data.pan.length > 0 && data.pan != '' && data.pan != null) {
			for (var i = 0; i < data.pan.length; i++) {
				var panData = data.pan[i];
				panArr.push(panData);
			}
		}
		if (data.lunshu != undefined && data.lunshu.length > 0 && data.lunshu != '' && data.lunshu != null) {
			for (var i = 0; i < data.lunshu.length; i++) {
				var lunshuData = data.lunshu[i];
				lunshuArr.push(lunshuData);
			}
		}
		if (data.shitu != undefined && data.shitu.length > 0 && data.shitu != '' && data.shitu != null) {
			for (var i = 0; i < data.shitu.length; i++) {
				var shituData = data.shitu[i];
				shituArr.push(shituData);
			}
		}
		if (data.paixu != undefined && data.paixu.length > 0 && data.paixu != '' && data.paixu != null) {
			for (var i = 0; i < data.paixu.length; i++) {
				var paixuData = data.paixu[i];
				paixuArr.push(paixuData);
			}
		}
		if (data.shipin != undefined && data.shipin.length > 0 && data.shipin != '' && data.shipin != null) {
			for (var i = 0; i < data.shipin.length; i++) {
				var shipinData = data.shipin[i];
				shipinArr.push(shipinData);
			}
		}
		if (data.anli != undefined && data.anli.length > 0 && data.anli != '' && data.anli != null) {
			for (var i = 0; i < data.anli.length; i++) {
				var anliData = data.anli[i];
				anliArr.push(anliData);
			}
		}
		if (data.chuan != undefined && data.chuan.length > 0 && data.chuan != '' && data.chuan != null) {
			for (var i = 0; i < data.chuan.length; i++) {
				var chuanData = data.chuan[i];
				chuanArr.push(chuanData);
			}
		}
		if (data.tk != undefined && data.tk.length > 0 && data.tk != '' && data.tk != null) {
			for (var i = 0; i < data.tk.length; i++) {
				var tkData = data.tk[i];
				tkArr.push(tkData);
			}
		}
		if (data.tb != undefined && data.tb.length > 0 && data.tb != '' && data.tb != null) {
			for (var i = 0; i < data.tb.length; i++) {
				var tbData = data.tb[i];
				tbArr.push(tbData);
			}
		}
		if (data.sc != undefined && data.sc.length > 0 && data.sc != '' && data.sc != null) {
			for (var i = 0; i < data.sc.length; i++) {
				var scData = data.sc[i];
				scArr.push(scData);
			}
		}
		dataArr = dataArr.concat(danArr, duoArr, panArr, lunshuArr, shituArr, paixuArr, shipinArr, anliArr, chuanArr, tkArr, tbArr, scArr);
		return dataArr;
	}
}

//认证模块考试题目类型名称
function CertificationQuestionTypeName(id) {
	var num = parseInt(id);
	var typeName = '';
	switch(num) {
		case 1:
			typeName = '单选题';
			break;
		case 2:
			typeName = '多选题';
			break;
		case 3:
			typeName = '判断题';
			break;
		case 4:
			typeName = '论述题';
			break;
		case 5:
			typeName = '上传题';
			break;
		case 7:
			typeName = '识图题';
			break;
		case 8:
			typeName = '流程排序题';
			break;
		case 9:
			typeName = '视频题';
			break;
		case 10:
			typeName = '案例分析题';
			break;
		case 11:
			typeName = '流程排序题';
			break;
		case 12:
			typeName = '填表题';
			break;
		case 13:
			typeName = '实操题';
			break;
		default:
	}
	return typeName;
}

//得到答案数据
function GetAnswerData(slideNumber) {
	var duooptionid = '';
	//多选题选项id
	var danoptionid = '';

	var duoObject = new Object();

	var Itemindex = '';
	//论述题id
	var duoQuestionidArr = new Array();
	var lunshuContent = '';
	var tkQuestionArr = new Array();
	//题目类型
	var itemtype = $(".swiper-wrapper").children().eq(slideNumber).attr('itemtype');
	var qid = $(".swiper-wrapper").children().eq(slideNumber).attr('qid');
	//获取题目类型
	if (itemtype == '1' || itemtype == '2' || itemtype == '3' || itemtype == '5' || itemtype == '7' || itemtype == '9' || itemtype == '8') {//单选题,多选题,判断题
		$(".swiper-wrapper").children().eq(slideNumber).find('input').each(function(i) {
			if ($(this).attr('type') === 'radio') {
				if (this.checked == true) {
					danoptionid = $(this).attr('dataindex');
					daan[qid] = danoptionid;
					$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-normal').addClass('qusetion-num-selected');
				}
			} else if ($(this).attr('type') === 'checkbox') {
				Itemindex = $(this).attr('Itemindex');
				if (this.checked == true) {
					duooptionid = $(this).attr('dataindex');
					duoObject[duooptionid] = "1";
					$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-normal').addClass('qusetion-num-selected');
				} else {
					duooptionid = $(this).attr('dataindex');
					duoObject[duooptionid] = "";
					delete duoObject[duooptionid];
				}
				if (JSON.stringify(duoObject) == "{}") {
					$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-selected').addClass('qusetion-num-normal');
				}
				daan[qid] = duoObject;
			}
		});
	} else if (itemtype == '4' || itemtype == '10' || itemtype == '12') {
		$(".swiper-wrapper").children().eq(slideNumber).find('textarea').on('input', function() {
			lunshuContent = $(this).val();
			daan[qid] = lunshuContent;
			if (lunshuContent != null && lunshuContent != '') {
				$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-normal').addClass('qusetion-num-selected');
			} else {
				$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-selected').addClass('qusetion-num-normal');
			}
		});
	} else {
		//	if(itemtype == '11'){//填空题
		var input = $(".swiper-wrapper").children().eq(slideNumber).find("input[name=" + qid + "]");
		input.each(function(i) {
			input.eq(i).blur(function() {
				var user_answer = '';
				var user_answer_flag = false;
				input.eq(i).parent().find('.tiankong').each(function() {
					var value = $.trim($(this).val());
					if (value) {
						user_answer_flag = true;
					}
					user_answer += value + '|';
				});
				user_answer = user_answer.replace(/(\|$)/g, "");
				if (user_answer_flag == false) {
					$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-selected').addClass('qusetion-num-normal');
				} else {
					$("#scroll-card").find("span").eq(slideNumber).removeClass('qusetion-num-normal').addClass('qusetion-num-selected');
				}
				daan[qid] = user_answer;
			});
		});
		//	}else{
		//	}

	}
}

//判断题返回结果
function panxuanzeReturnStr(i) {
	var num = (i + 1) % 2;
	switch(num) {
		case 0:
			return '错误'
			break;
		case 1:
			return '正确'
			break;
		default:
			return '暂无'
			break;
	}
}

//选项是否有图片
function xuanzheImgIsNull(xuanzheStr) {

	if (!isEmpty(xuanzheStr)) {//判断题目选项是否为空
		if (xuanzheStr.toString().indexOf("img") > 0) {//题目选项中带有图片
			var ImgArr = RegularGetImgSrcArr(xuanzheStr);
			//获取题目选项中的图片路径数组
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(xuanzheStr))) {//去除样式标签后的题目选项字符串不为空时
				words = htmlspecialchars_decode(xuanzheStr);
			} else {
				words = '<br>';
			}
			if (ImgArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < ImgArr.length; i++) {
					var imgstr2 = PicDomainName(ImgArr[i], 0);
					var imgstr3 = '<div class="option_video_div"  imgUrl="' + ImgArr[i] + '" onclick="showImg(this)">' + '<img class="xuanzeImgDiv" src="' + imgstr2 + '"  imgUrl="' + ImgArr[i] + '" alt="" >' + '</div>';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var xuanzheImgstr = PicDomainName(ImgArr[0], 0);
				return words + '<div class="option_video_div"  imgUrl="' + ImgArr[0] + '" onclick="showImg(this)">' + '<img class="xuanzeImgDiv" src="' + xuanzheImgstr + '"  imgUrl="' + ImgArr[0] + '" alt="" >' + '</div>';
			}

		} else if (xuanzheStr.toString().indexOf("edui-video-resource") > 0 || xuanzheStr.toString().indexOf("video") > 0) {//题目中带有视频
			var VideoUrlArr = xuanzheStr.indexOf("edui-video-resource") > 0 ? RegularGetVideoUrlArr(xuanzheStr) : RegularGetVideotwoUrlArr(xuanzheStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(xuanzheStr))) {
				words = htmlspecialchars_decode(xuanzheStr);
			} else {
				words = '<br>';
			}
			if (VideoUrlArr.length > 1) {//视频数量超过1个
				var ImgStr1 = "";
				for (var i = 0; i < VideoUrlArr.length; i++) {
					var imgstr2 = VideoUrlArr[i];
					var imgstr3 = '<div class="option_video_div"  video-url="' + VideoUrlArr[i] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var xuanzheStr = VideoUrlArr[0];
				return words + '<div class="option_video_div"  video-url="' + VideoUrlArr[0] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
			}

		} else {
			return '<label>' + htmlspecialchars_decode(xuanzheStr);
			+'</label>';
		}
	} else {
		return '';
	}
}

//题目是否有图片
function questionsItemIsNull(questionsStr) {
	if (!isEmpty(questionsStr)) {
		if (questionsStr.indexOf("img") > 0) {//题目存在图片
			var ImgArr = RegularGetImgSrcArr(questionsStr);
			//获取题目选项中的图片路径数组

			var words = "";
			if (!isEmpty(htmlspecialchars_decode_question(questionsStr))) {//去除样式标签后的题目字符串不为空时
				words = htmlspecialchars_decode_question(questionsStr);
			} else {
				words = '<br>';
			}
			if (ImgArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < ImgArr.length; i++) {
					var imgstr2 = PicDomainName(ImgArr[i], 320);
					var imgstr3 = '<img class="questionItemImgDiv"  src="' + imgstr2 + '" imgUrl="' + ImgArr[i] + '"  alt="" onclick="showImg(this)">';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var questionsItemImgstr = PicDomainName(ImgArr[0], 320);
				return words + '<img class="questionItemImgDiv"  src="' + questionsItemImgstr + '" imgUrl="' + ImgArr[0] + '"  alt="" onclick="showImg(this)">';
			}
		} else if (questionsStr.toString().indexOf("edui-video-resource") > 0 || questionsStr.toString().indexOf("video") > 0) {//题目中存在视频

			var VideoUrlArr = questionsStr.indexOf("edui-video-resource") > 0 ? RegularGetVideoUrlArr(questionsStr) : RegularGetVideotwoUrlArr(questionsStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode_question(questionsStr))) {
				words = htmlspecialchars_decode_question(questionsStr);
			} else {
				words = '<br>';
			}
			if (VideoUrlArr.length > 1) {//视频数量大于1
				var ImgStr1 = "";
				for (var i = 0; i < VideoUrlArr.length; i++) {
					var imgstr2 = VideoUrlArr[i];
					var imgstr3 = '<div class="question_video_div"  video-url="' + VideoUrlArr[i] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var questionsItemstr = VideoUrlArr[0];
				return words + '<div class="question_video_div"  video-url="' + VideoUrlArr[0] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
			}
		} else {
			return htmlspecialchars_decode_question(questionsStr);
		}
	} else {
		return '';
	}
}

//正则获取img标签中的src地址
function RegularGetImgSrcArr(str) {
	var srcArr = new Array();
	var imgReg = /<img.*?(?:>|\/>)/gi;
	//匹配src属性
	var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
	var arr = str.match(imgReg);
	for (var i = 0; i < arr.length; i++) {
		var src = arr[i].match(srcReg);
		srcArr.push(getpicurl(src[1]));
	}
	return srcArr;
}

//正则获取p标签中的url地址
function RegularGetVideoUrlArr(str) {
	var UrlArr = new Array();
	var reg = /<p.*?class[ \t]*=[ \t]*"edui-video-resource".*?>.*?<\/p>/ig;
	//匹配url属性
	var urlReg = /url=[\'\"]?([^\'\"]*)[\'\"]?/i;
	var arr = str.match(reg);
	for (var i = 0; i < arr.length; i++) {
		var src = arr[i].match(urlReg);
		UrlArr.push((src[1]));
	}
	return UrlArr;
}

//正则获取p标签中的url地址
function RegularGetVideotwoUrlArr(str) {
	var UrlArr = new Array();
	var reg = /<source.*?(?:>|\/>)/gi;
	//匹配url属性
	var urlReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
	var arr = str.match(reg);
	for (var i = 0; i < arr.length; i++) {
		var src = arr[i].match(urlReg);
		UrlArr.push((src[1]));
	}

	return UrlArr;
}

//展示图片
function showImg(obj) {

	openLocalFile(obj.attributes["imgUrl"].nodeValue)
}

//播放视频
function toPlayVideo(obj) {
	var url = obj.attributes["video-url"].nodeValue;
	if (!isEmpty(url)) {
		api.openWin({useWKWebView:true,
			name : 'BaiDuWebVideo',
			url : '../Study/BaiDuWebVideo.html',
			pageParam : {
				title : '',
				url : url,
				token : '',
			},
			slidBackEnabled : false,
			reload : true
		});
	} else {
		ShowToast('暂无视频')
	}
}

//	生成答题卡题号
function popoverCardNum(questionLen) {
	for (var i = 0; i < questionLen; i++)
		$("#scroll-card").append('<span class="qusetion-num-normal">' + (i + 1) + '</span>')
}

//测验结果生成答题卡
function TestScorePopoverCardNum(questionLen) {
	for (var i = 0; i < questionLen; i++) {
		$("#scroll-card").append('<span class="qusetion-num-selected" >' + (i + 1) + '</span>')
	}
}

//初始化Swipe组件
function InitmySwiper() {
	var mySwiper = new Swiper('.swiper-container', {
		autoHeight : true,
		//用户操作swiper之后，是否禁止autoplay
		autoplayDisableOnInteraction : true,
		direction : 'horizontal',
		loop : false,
	})
	// 点击答题卡切换
	$("#popover-card span").on("click", function() {
		var index = $(this).index();
		mySwiper.slideTo(index, 1000, false);
		mui('#popover-card').popover('hide')
	});
}

//设置考试初始化代码
function SetTestExamUI(data) {
	var questionLen = data.length;
	//	生成答题卡题号
	popoverCardNum(questionLen);
	var TestHtml = "";
	var htmlStr = "";
	for (var i = 0; i < questionLen; i++) {
		if (!isEmpty(data[i])) {
		     
			htmlStr = GetQuestionHtml(data[i], data.length, i);
             
		} else {
			htmlStr = "";
		}

		TestHtml = TestHtml + htmlStr;
	}

	return TestHtml;
}

//判断当前的题目分数字段
function judgeQuestionScore(list) {
	var scoreStr = "";
	if (!isEmpty(list)) {
		if (list.hasOwnProperty('score')) {
			return list.score;
		} else if (list.hasOwnProperty('qfenshu')) {
			return list.qfenshu;
		} else {

			return ''
		}

	} else {
		return ''
	}
}

//得到每道题目题目ui
function GetQuestionHtml(data, len, i) {
	var qxuan = qxuanxiangIsNull(data.qxuanxiang);
	var scoreStr = (i + 1) + '/' + len + "." + "(" + judgeQuestionScore(data) + "分)";
	var questionStr = "";
	if (data.qtype == 11) {//填空题
		questionStr = FillInBlankQuestionStr(data)
	} else if (data.qtype == 13) {//实操题
		questionStr = questionsItemIsNull(data.questions)+'<br>'+PracticalQuestionStr(data.qid);
		
	} else {
		questionStr = questionsItemIsNull(data.questions)
	}
	
	var qutestionItemHtml = GetquestionItemStr(qxuan, data, i);
	return '<div class="swiper-slide" id="item' + (i + 1) + '" itemtype="' + data.qtype + '" qid="' + data.qid + '" onclick="GetAnswerData(' + i + ')">' 
	+ '<div class="k-con-part3 clearfix question_nomal">' 
	+ '<span id="itemType" class="k-f18">' + CertificationQuestionTypeName(data.qtype) + '</span>' 
	+ '<p class="k-f16 k-c323 k-mt5 indent-5 question_p">' + scoreStr + questionStr + '</p>' 
	+ GetquestionMessageStr(data.qtype) 
	+ '<div class="test-answer-list left_10"  id="question_item' + data.qid + '"  >' + qutestionItemHtml + '</div>' 
	+ '</div>' 
	+ '</div>';
}

//得到题目选项html
function GetquestionItemStr(qxuan, data, i) {
	var questionItemHtml = "";
	var xuanzeStr = "";
	var questionItemStr = "";
	var len = "";
	if (data.qtype == 4 || data.qtype == 10) {
		len = 1;
	} else {
		len = qxuan.length;
	}
	for (var k = 0; k < len; k++) {
		var xuanzeStr1 = data.qtype != 3 ? qxuan[k].xuanzhe : panxuanzeReturnStr(k);
		xuanzeStr = xuanzheImgIsNull(xuanzeStr1);
		if (data.qtype == 1 || data.qtype == 3 || data.qtype == 8) {
			questionItemStr = '<div class="mui-input-row mui-radio mui-left " >'
			// +  '<label>' + xuanzeStr + '</label>'
			+xuanzeStr + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="radio' + data.qid + '" type="radio"   itemid="' + data.qid + '">' + '</div> '

		} else if (data.qtype == 2 || data.qtype == 7) {
			questionItemStr = '<div class="mui-input-row mui-checkbox mui-left">'
			// +  '<label>' + xuanzeStr + '</label>'
			+xuanzeStr + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1" type="checkbox"  itemid="' + data.qid + '">' + '</div>'
		} else if (data.qtype == 4 || data.qtype == 10 || data.qtype == 12) {
			questionItemStr = '<textarea Itemindex="' + i + '" class="k-texttarea k-f14" id="content" itemid="' + data.qid + '"></textarea>';
		} else if (data.qtype == 5 || data.qtype == 13) {
			questionItemStr = "";
		} else if (data.qtype == 9) {//视频题
			questionItemStr = '<div class="mui-input-row mui-checkbox mui-left">'
			// +  '<label>' + xuanzeStr + '</label>'
			+xuanzeStr + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1" type="checkbox"  itemid="' + data.qid + '">' + '</div>';

		} else {
			questionItemStr = "";
		}
		questionItemHtml = questionItemHtml + questionItemStr;
	}
	return questionItemHtml
}

//题目附加信息
function GetquestionMessageStr(qtype) {
	var questionHtml = "";
	if (qtype == 5) {
		questionHtml = '<p class="k-cred left_10 " style="color:red">' + "移动端暂不支持上传题，请在PC端查看！" + '</p>'
	} else if (qtype == 9) {
		questionHtml = ''
	} else if (qtype == 13) {//实操题

	} else {
		questionHtml = "";
	}

	return questionHtml;
}

//返回填空题
function FillInBlankQuestionStr(data) {
	var str = '<input class="tiankong"  type="text"  qid="' + data.qid + '" name="' + data.qid + '" value="">';
	var nstr = data.questions.replace(/\([^\)]*\)/g, str);
	var nstr2 = nstr.replace(/\（.*?\）/g, str);
	return deletSTYLE(nstr2)

}

function PracticalQuestionStr(qid) {
	var str = '<div id="rz_attachmentTableView' + qid + '" class="weui_cells fileDownLoad_Div"></div>'
    return str
}

function deletSTYLE(str) {
	if (str != null && str != "") {
		//			 str =str.replace(/style="[^"]*"/g,"");
		//			  str.replace(/style\s*=(['\"\s]?)[^'\"]*?\1/gi,"");
		return str;
	} else {
		return str;
	}
}

//判断当前的题目分数字段
function judgeQuestiondaan(list) {

	var scoreStr = "";
	if (!isEmpty(list)) {
		if (list.hasOwnProperty('qdaan') && !isEmpty(list.qdaan)) {

			return list.qdaan;
		} else if (list.hasOwnProperty('qjiexi') && !isEmpty(list.qjiexi)) {
			return list.qjiexi;
		} else {

			return ''
		}

	} else {
		return ''
	}
}

